<?php

namespace app\admin\model\booking;

use app\admin\model\booking\HouseForm;
use Exception;
use think\Model;


class House extends Model
{

    // 表名
    protected $name = 'booking_house';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'images_text',
        'image_text'
    ];

    protected static function init()
    {
        self::afterWrite(function ($row) {
            //更新自定义字段值
            if (isset($row['fields']) && !empty($row['fields'])) {
                $data = $row['fields'];
                $data['house_id'] = $row['id'];
                HouseForm::updateDiyData($data);
            }
        });

        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });

        self::beforeWrite(function ($row) {
            if ($row['price'] >= $row['market_price']) {
                throw new \Exception('市场价格必须大于实际价格');
            }
            if ($row['to_shop_pay'] == 1 && $row['interval_time'] < $row['checkin_time']) {
                throw new Exception('到店付取消订单时间不能小于入住时间');
            }
            if (isset($row['interval_time'])) {
                $row['interval_time'] = $row['to_shop_pay'] == 1 ? $row['interval_time'] : null;
            }
        });
    }


    public function getStatusList()
    {
        return ['audit' => __('Status audit'),'up' => __('Status up'), 'down' => __('Status down')];
    }

    public function getToShopPayList()
    {
        return ['0' => __('To_shop_pay 0'), '1' => __('To_shop_pay 1')];
    }

    public function getImagesTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['images'] ?? '');
        return array_filter(explode(',', $value));
    }

    public function getImageTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['images'] ?? '');
        $arr = array_filter(explode(',', $value));
        return $arr[0] ?? '';
    }

    public function getTagNamesAttr($value, $data)
    {
        $value = $value ? $value : ($data['tag_names'] ?? '');
        if (!$value) {
            return [];
        }
        return array_filter(explode(',', $value));
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['status'] ?? '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getToShopPayTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['to_shop_pay']) ? $data['to_shop_pay'] : '');
        $list = $this->getToShopPayList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getGroupNameAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['group_name']) ? $data['group_name'] : '');
        if (!$value) {
            return [];
        }
        $value = explode(',', $value);
        $arr = [];
        foreach ($value as $item) {
            $a = explode('|', $item);
            if (isset($a[1]) && !empty($a[1])) {
                $arr[$a[1]][] = $a[0];
            }
        }
        return $arr;
    }

    public function setNotesInAttr($value, $data)
    {
        $value = !$value ? [] : $value;
        return is_array($value) ? json_encode($value) : $value;
    }

    public function Store()
    {
        return $this->belongsTo('Store', 'store_id', 'id', [], 'LEFT');
    }

    public function HouseForm()
    {
        return $this->belongsTo('HouseForm', 'id', 'house_id', [], 'LEFT');
    }

    public function typeConfig()
    {
        return $this->belongsTo('Config', 'type_config_id', 'id', [], 'LEFT');
    }

    public function floorConfig()
    {
        return $this->belongsTo('Config', 'floor_config_id', 'id', [], 'LEFT');
    }

    // public static function refresFields($source_id)
    // {
    //     $fields = Fields::where('source', 'house')->where('source_id', $source_id)->field('name')->column('name');
    //     self::where('id', $source_id)->update(['applyfields' => implode(',', $fields)]);
    // }

}
